package com.ff.rest;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ff.base.StandardResult;
import com.ff.service.OssService;
import com.google.common.collect.Maps;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * <p>
 * Description: OSS文件上传，上传成功后会返回图片的URL，因为是我自己的OSS，是私有的，没有充钱，所以返回的URL不能被访问。替换为公司的即可
 * </p>
 * 
 * @author: zsljava
 * @version 1.0.0
 * <p>
 * History:
 * -----------------------------------------------
 * @Date: 2017年7月14日 下午5:05:52
 * @author: zsljava
 * @version 1.0.0
 * @OP: Create
 * -----------------------------------------------
 * </p>
 *
 * @since
 * @see
 */
@Api(description = "文件上传", tags = {"aa"}, value = "AAA")
@RestController
public class FileRouter {

	@Autowired
	private OssService ossService;

	@ApiOperation(value = "OSS文件上传", notes = "OSS文件上传接口")
	@RequestMapping(path = "/upload", method = RequestMethod.POST)
	public StandardResult uploadFile(MultipartFile file) {
		String[] urls = ossService.uploadFiles(file);
		return new StandardResult(urls);
	}
	
	@ApiOperation(value = "OSS私有文件临时授权url", notes = "OSS私有文件临时授权url")
	@RequestMapping(path = "/file/{key}", method = RequestMethod.GET)
	public StandardResult getPrivatePicUri(@ApiParam(required=true, name="key", value="文件路径") @PathVariable String key) {
		String url = ossService.getPrivatePicUri(key);
		return new StandardResult(url);
	}

	@ApiOperation(value = "OSS多文件上传", notes = "OSS多文件上传接口")
	@RequestMapping(path = "/uploads", method = RequestMethod.POST)
	public StandardResult uploadFiles(@RequestParam(value = "files") MultipartFile[] files) throws Exception {
		Map<String, MultipartFile> map = Maps.newHashMap();
		if (files != null) {
			for (MultipartFile multipartFile : files) {
				map.put(multipartFile.getOriginalFilename(), multipartFile);
			}
		}

		String[] picPaths = new String[map.size()];
		if (map.size() > 0) {
			picPaths = ossService.uploadFiles(map);
		} else {
			throw new Exception("没有文件");
		}

		return new StandardResult(picPaths);
	}
	
	@ApiOperation(value = "OSS文件上传生成授权", notes = "web前端直接上传到oss，不经服务器周转")
	@RequestMapping(path = "/uploads/policy", method = RequestMethod.GET)
	public StandardResult createPolicy() {
		String result = null;
		try {
			result = ossService.createPolicy();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new StandardResult(result);
	}
	
	@ApiOperation(value = "OSS Callback", notes = "阿里云回调接口")
	@RequestMapping(path = "/oss/callback", method = RequestMethod.POST)
	public StandardResult callback(@RequestParam(value = "filename") String filename, HttpServletRequest request) {
		System.out.println("----------------------");
		System.out.println(request.getHeader("Authorization"));
		System.out.println(filename);
		
		return new StandardResult("success");
	}

}